package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.PageResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbContentCategory")
public class TbContentCategoryController {

    //注入service
    @DubboReference
    private TbContentCategoryService tbContentCategoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    //查询全部广告分类列表(要求分页)
    @ResponseBody
    @RequestMapping("TbContentCategoryshowall")
    public Result TbContentCategoryshowall(String name, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
        try {
            PageResult<TbContentCategory> tbBrands = tbContentCategoryService.TbContentCategoryshowall(pageNum, pageSize,name);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(tbBrands);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //查询全部广告分类列表(用于回显)
    @ResponseBody
    @RequestMapping("TbContentCategoryshow")
    public Result TbContentCategoryshow() {
        try {
            List<TbContentCategory> tbBrands = tbContentCategoryService.TbContentCategoryshow();
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(tbBrands);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //添加广告分类
    @ResponseBody
    @RequestMapping("addTbContentCategory")
    public Result addTbContentCategory(TbContentCategory contentCategory) {
        try {
            tbContentCategoryService.addTbContentCategory(contentCategory);
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //回显单个广告分类数据
    @ResponseBody
    @RequestMapping("showecho")
    public Result showecho(Long id) {
        try {
            TbContentCategory tbBrand = tbContentCategoryService.showecho(id);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(tbBrand);
        } catch (Exception e) {
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }

    //修改单个数据
    @ResponseBody
    @RequestMapping("updateTbContentCategory")
    public Result updateTbContentCategory(TbContentCategory tbBrand) {
        try {
            tbContentCategoryService.updateTbContentCategory(tbBrand);
            redisTemplate.delete("shuju");
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }

    //删除单个数据
    @ResponseBody
    @RequestMapping("deleteTbContentCategory")
    public Result deleteTbContentCategory(Long id) {
        try {
            tbContentCategoryService.deleteTbContentCategory(id);
            redisTemplate.delete("shuju");
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //删除多个数据
    @ResponseBody
    @RequestMapping("deleteallTbContentCategory")
    public Result deleteallTbContentCategory(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbContentCategoryService.deleteallTbContentCategory(ids);
            redisTemplate.delete("shuju");
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }
}
